package com.auth.controller;

import cn.hutool.http.HttpUtil;
import com.common.model.po.SysUser;
import com.auth.service.UserService;
import com.common.model.util.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class UserController {
 
    @Value("${server.port}")
    private int port;

    @Autowired
    private UserService userService;

    @PostMapping(value = "register")
    public AjaxResult register(@RequestBody SysUser user) {
        SysUser mdsUser = userService.register(user);
        if (mdsUser == null) {
            return AjaxResult.error("注册失败,用户名已存在");
        }
        return AjaxResult.ok(mdsUser);
    }

    @PostMapping(value = "login")
    public String login(@RequestBody SysUser user) {
        Map<String, Object> map = new HashMap<>();//存放参数
        map.put("client_id", "client1");
        map.put("client_secret", "123123");
        map.put("grant_type", "password");
        map.put("username", user.getUsername());
        map.put("password", user.getPassword());
        String body = HttpUtil.createPost("http://127.0.0.1:18080/" + "authserver/oauth/token").form(map).execute().body();
        return body;
    }

}